local chengyong = fk.CreateSkill{
    name = "ym_water__chengyong"
}

Fk:loadTranslationTable{
    ["ym_water__chengyong"] = "逞勇",
    [":ym_water__chengyong"] = "结束阶段，或造成伤害后，你可令下次受到或造成的伤害翻倍，可令下次摸或弃置的牌翻倍。",
    ["#ym_water__chengyong-prompt"] = "逞勇：你可以选择令下次受到或造成的伤害翻倍，或令下次摸或弃置的牌翻倍",
    ["#ym_water__chengyong-choice"] = "逞勇：请选择一项：令下次受到或造成的伤害翻倍；令下次摸或弃置的牌翻倍",
    ["ym_water__chengyong_damage"] = "令下次受到或造成的伤害翻倍",
    ["ym_water__chengyong_draw"] = "令下次摸或弃置的牌翻倍",
    ["ym_water__chengyong_discard"] = "令下次弃置的牌翻倍",
    ["@@ym_water__chengyong_damage"]="伤害翻倍",
    ["@@ym_water__chengyong_damage"]="卡牌翻倍",
}

-- 结束阶段触发
chengyong:addEffect(fk.EventPhaseStart, {
    can_trigger = function(self, event, target, player, data)
        return player:hasSkill(self.name) and target == player and player.phase == Player.Finish
    end,
    on_cost = function(self, event, target, player, data)
        local room = player.room

        local choices = {}
        if player:getMark("@@ym_water__chengyong_damage")==0 then
            table.insert(choices,"ym_water__chengyong_damage")
        end
        if player:getMark("@@ym_water__chengyong_draw")==0 then
            table.insert(choices,"ym_water__chengyong_draw")
        end
        if #choices==0 then
            return
        end
        local choice = room:askForChoice(player, choices, self.name, "#ym_water__chengyong-choice", false)
        if choice then
            event:setCostData(self, choice)
            return true
        end
        return false
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local choice = event:getCostData(self)
        
        if choice == "ym_water__chengyong_damage" then
            room:setPlayerMark(player, "@@ym_water__chengyong_damage", 1)
        elseif choice == "ym_water__chengyong_draw" then
            room:setPlayerMark(player, "@@ym_water__chengyong_draw", 1)
        end
    end,
})

-- 造成伤害后触发
chengyong:addEffect(fk.Damage, {
    can_trigger = function(self, event, target, player, data)
        return player:hasSkill(self.name) and target == player
    end,
    on_cost = function(self, event, target, player, data)
        local room = player.room
        local choices = {"ym_water__chengyong_damage", "ym_water__chengyong_draw"}
        local choice = room:askForChoice(player, choices, self.name, "#ym_water__chengyong-choice", false)
        if choice then
            event:setCostData(self, choice)
            return true
        end
        return false
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local choice = event:getCostData(self)
        
        if choice == "ym_water__chengyong_damage" then
            room:setPlayerMark(player, "@@ym_water__chengyong_damage", 1)
        elseif choice == "ym_water__chengyong_draw" then
            room:setPlayerMark(player, "@@ym_water__chengyong_draw", 1)
        end
    end,
})

-- 伤害翻倍效果
chengyong:addEffect(fk.DamageCaused, {
    can_trigger = function(self, event, target, player, data)
        return target and target:getMark("@@ym_water__chengyong_damage") > 0 and player:hasSkill(self.name)
    end,
    on_cost=Util.TrueFunc,

    on_use = function(self, event, target, player, data)
        local room = target.room
        room:setPlayerMark(target, "@@ym_water__chengyong_damage", 0)
        data:changeDamage(data.damage)
    end,
})


chengyong:addEffect(fk.DamageInflicted, {
    can_trigger = function(self, event, target, player, data)
        return target:getMark("@@ym_water__chengyong_damage") > 0 and player:hasSkill(self.name)
    end,
    on_cost=Util.TrueFunc,

    on_use = function(self, event, target, player, data)
        local room = target.room
        room:setPlayerMark(target, "@@ym_water__chengyong_damage", 0)
        data:changeDamage(data.damage)

    end,
})

-- 摸牌翻倍效果
chengyong:addEffect(fk.BeforeDrawCard, {
    can_trigger = function(self, event, target, player, data)
        return target:getMark("@@ym_water__chengyong_draw") > 0 and data.num > 0
    end,
    on_cost=Util.TrueFunc,
    on_use = function(self, event, target, player, data)
        local room = target.room
        room:setPlayerMark(target, "@@ym_water__chengyong_draw", 0)
        data.num = data.num * 2
    end,
})

-- 弃牌翻倍效果
chengyong:addEffect(fk.AfterCardsMove, {
    anim_type = "drawcard",
    can_trigger = function(self, event, target, player, data)
      if player:hasSkill(chengyong.name) and player:getMark("@@ym_water__chengyong_draw") > 0 then
        local yes = false
        local x=0
        for _, move in ipairs(data) do
          if move.from == player and move.moveReason == fk.ReasonDiscard and move.skillName ~= chengyong.name then
            yes=true
            for _, info in ipairs(move.moveInfo) do
                if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
                    x=x+1
                end
            end
          end
        end
        if yes then
            event:setCostData(self, x)
            return true
        end
      end
    end,
    on_cost = function (self, event, target, player, data)
        return true
    end,
    on_use = function (self, event, target, player, data)
      local room = player.room
      local x = event:getCostData(self)
      local cards=player:getCardIds("he")
      local num=math.max(x,#cards)
      local cids=table.random(cards,num)
      room:throwCard(cids, chengyong.name, player, player)
    end,
  })

return chengyong

